<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head th:include="includes/add-include :: add-header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal" id="myForm">
        <div class="form-group">
            <label class="col-sm-3 control-label">原密码</label>
            <div class="col-sm-5">
                <input type="password" name="password" placeholder="原密码"
                       class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">新密码</label>
            <div class="col-sm-5">
                <input type="password" name="newPassword" placeholder="新密码"
                       class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">确认密码</label>
            <div class="col-sm-5">
                <input type="password" name="confirmPassword"
                       placeholder="确认密码" class="form-control">
            </div>
        </div>
    </form>
    </div>
    <div th:include="includes/add-include :: add-footer"></div>
    <script type="text/javascript">
	    var prefix = ctx + "system/user";

	    $(function () {
            var options = {
                fields:{
                    password:{
                        message:'密码非法',
                        validators:{
                            //非空
                            notEmpty:{
                                message:'必填'
                            },
                            //限制字符串长度
                            stringLength:{
                                min:3,
                                max:20,
                                message:'密码长度必须介于3到20之间'
                            },
                            //基于正则表达式的验证
                            regexp:{
                                regexp:/^[a-zA-Z0-9_\.]+$/,
                                message:'密码由数字字母下划线和.组成'
                            }
                        }
                    },
                    newPassword:{
                        message:'新密码非法',
                        validators:{
                            //非空
                            notEmpty:{
                                message:'必填'
                            },
                            //限制字符串长度
                            stringLength:{
                                min:3,
                                max:20,
                                message:'新密码长度必须介于3到20之间'
                            },
                            //基于正则表达式的验证
                            regexp:{
                                regexp:/^[a-zA-Z0-9_\.]+$/,
                                message:'新密码由数字字母下划线和.组成'
                            }
                        }
                    },
                    confirmPassword:{
                        message:'密码非法',
                        validators:{
                            //非空
                            notEmpty:{
                                message:'必填'
                            },
                            //限制字符串长度
                            stringLength:{
                                min:3,
                                max:20,
                                message:'密码长度必须介于3到20之间'
                            },
                            //相同性检测
                            identical:{
                                field:'newPassword',
                                message:'两次密码输入不一致'
                            },
                            //基于正则表达式的验证
                            regexp:{
                                regexp:/^[a-zA-Z0-9_\.]+$/,
                                message:'密码由数字字母下划线和.组成'
                            }
                        }
                    }
                }
            }
            $.validate.init(options);
        })

        function submitHandler(){
            if(!$.validate.form()){
                return;
            }
            var item = $("#myForm").serializeJSON();
            $.modal.loading("正在处理中，请稍后...");
            var config = {
                url: prefix + "/editPW",
                type: 'post',
                dataType: 'json',
                contentType:'application/json;charset=utf-8',
                data: JSON.stringify(item),
                success: function (result) {
                    if(result.code == web_status.SUCCESS){
                        $.modal.msgSuccess(result.msg);
                        location.href = "/login";
                    }else{
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                }
            }
            $.ajax(config);
        }
    </script>
</body>
</html>
